   package com.javalong.affairs.module.schedule.fragment;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.javalong.affairs.R;
import com.javalong.affairs.base.fragment.BaseFragment;
import com.javalong.affairs.module.notifything.activity.EditContentActivity;
import com.javalong.affairs.base.constant.BundleKey;
import com.javalong.affairs.base.constant.RequestCode;
import com.javalong.affairs.base.constant.RequestKey;
import com.javalong.affairs.base.listener.BaseResponseListener;
import com.javalong.affairs.base.manager.IntentManager;
import com.javalong.affairs.base.ui.TextSelectView;
import com.javalong.affairs.base.ui.TextSelectView.OnImageClick;
import com.javalong.affairs.base.ui.TopTitle;
import com.javalong.affairs.base.utils.DateTimePickDialogUtil;
import com.javalong.affairs.base.utils.DateUtils;
import com.javalong.affairs.base.utils.HttpUtils;
import com.javalong.affairs.base.utils.UrlUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

public class MyScheduleFragment extends BaseFragment implements OnImageClick,
		OnClickListener {
	@ViewInject(R.id.ui_toptitle)
	private TopTitle topTitle;
	@ViewInject(R.id.tv_time)
	private TextView tv_time;
	@ViewInject(R.id.et_address)
	private EditText et_address;
	@ViewInject(R.id.et_content)
	private EditText et_content;
	@ViewInject(R.id.bt_send)
	private Button bt_send;
	@ViewInject(R.id.ui_tsview_receiver)
	private TextSelectView tsView_receiver;
	@ViewInject(R.id.et_title)
	private EditText et_title;
	@ViewInject(R.id.bt_starttime)
	private Button bt_starttime;
	@ViewInject(R.id.bt_endtime)
	private Button bt_endtime;
	@ViewInject(R.id.tv_starttime)
	private TextView tv_starttime;
	@ViewInject(R.id.tv_endtime)
	private TextView tv_endtime;
	private Date nowDate;
	private DateTimePickDialogUtil startDialog;
	private DateTimePickDialogUtil endDialog;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_myschedule, container,
				false);
		return view;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		ViewUtils.inject(this);
		nowDate = new Date();
		initUI();
	}

	private void initUI() {
		bt_send.setOnClickListener(this);
		bt_starttime.setOnClickListener(this);
		bt_endtime.setOnClickListener(this);
		startDialog = new DateTimePickDialogUtil(getActivity(),
				DateUtils.formatDate(nowDate));
		endDialog = new DateTimePickDialogUtil(getActivity(),
				DateUtils.formatDate(nowDate));

		et_content.setOnClickListener(this);

	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.bt_starttime:
			startDialog.dateTimePicKDialog(tv_starttime);
			break;
		case R.id.bt_endtime:
			endDialog.dateTimePicKDialog(tv_endtime);
			break;
		case R.id.bt_send:
			String url = UrlUtils.getUrl("Journal", "insert");
			Map<String, Object> params = new HashMap<String, Object>();
			params.put(RequestKey.TITLE, et_title.getText().toString());
			params.put(RequestKey.PLACE, et_address.getText().toString());
			params.put(RequestKey.CONTENT, et_content.getText().toString());
			if (startDialog.getSeconds() != 0) {
				params.put(RequestKey.TIMEBEGIN, startDialog.getSeconds());
			}
			if (endDialog.getSeconds() != 0) {                                                                                                                                                                                                                                                                                                                                                       
				params.put(RequestKey.TIMEEND, endDialog.getSeconds());
			}
			// params.put(RequestKey.ATTACHMENT,
			// et_timelimit.getText().toString());

			HttpUtils.post(url, params, new BaseResponseListener(null) {
				@Override
				public void onSuccess(Object obj) {
					super.onSuccess(obj);
					getActivity().finish();
				}
			});
			break;
		case R.id.et_content:
			Bundle bundle = new Bundle();
			bundle.putString(BundleKey.EDITCONTENT, et_content.getText()
					.toString());
			IntentManager.doActivityForResult(getActivity(),
					EditContentActivity.class, RequestCode.EDITCONTENT, bundle);
			break;
		default:
			break;
		}
	}

	public void setContent(String editContent) {
		et_content.setText(editContent);
	}

}
